package com.zzyl.nursing.controller.member.service.impl;

import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.controller.member.service.BindingFamilyMemberService;
import com.zzyl.nursing.dto.BindingElderInfoDto;
import com.zzyl.nursing.mapper.BindingElderInfoMapper;
import com.zzyl.nursing.mapper.ElderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class BindingFamilyMemberServiceImpl implements BindingFamilyMemberService {
    @Autowired
    private BindingElderInfoMapper bindingElderInfoMapper;
    @Autowired
    private ElderMapper elderMapper;
    /**
     * 绑定老人信息
     * @param bindingElderInfoDto
     */
    public void bindingFamilyMember(BindingElderInfoDto bindingElderInfoDto){
        //从threadLocal中获取用户id
        Long userId = UserThreadLocal.getUserId();
        bindingElderInfoDto.setCreateBy(userId);
        //获取创建时间
        bindingElderInfoDto.setCreateTime(LocalDateTime.now());
        //设置familyMemberId
        bindingElderInfoDto.setFamilyMemberId(userId);
        //获取老人姓名
        String name = bindingElderInfoDto.getName();
        //获取身份证号
        String idCard = bindingElderInfoDto.getIdCard();
        //到老人表中查询是否有该老人
        int i = elderMapper.selectElderByIdCardAndName(name, idCard);
        if (i == 0){
            //没有该老人，抛出异常
            throw new RuntimeException("没有该老人");
        }else {
            //根据老人姓名和身份证号查询老人id
            Long elderId = elderMapper.selectElderIdByNameAndIdCard(name, idCard);
            bindingElderInfoDto.setElderId(elderId);
            //有该老人，绑定
            bindingElderInfoMapper.bindingFamilyMember(bindingElderInfoDto);
        }


    }
    /**
     * 解绑老人信息
     * @param id
     */
    @Override
     public void deleteElderById(Long id){
        bindingElderInfoMapper.deleteElderById(id);
    }
}
